package com.open4cn.simulator.core.connector;

import cn.hutool.core.text.CharSequenceUtil;

import java.io.Serializable;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description 连接地址
 * @Email yinxiaopeng@zj.tech
 * @date 2022-03-15 00:02
 */
public class ConnectAddress implements Serializable {
    // ip地址
    private String host;
    // 端口:8080
    private Integer port;
    // COM口:COM2
    private String comPort;
    // 波特率
    private Integer baudRate;

    public ConnectAddress(Integer port) {
        this.port = port;
    }

    public ConnectAddress(String host, Integer port) {
        this.host = host;
        this.port = port;
    }

    public ConnectAddress(Integer baudRate, String comPort) {
        this.baudRate = baudRate;
        this.comPort = comPort;
    }

    /**
     * 对连接地址进行检查
     *
     * @param connectorMode
     * @return
     */
    public boolean check(ConnectorModeEnum connectorMode) {
        boolean success = false;
        switch (connectorMode) {
            case TCP_CLIENT:
            case UDP_SERVER_CLIENT:
                success = CharSequenceUtil.isNotBlank(host) && null != port;
                break;
            case TCP_SERVER:
                success = null != port;
                break;
            case SERIAL_PORT:
                success = CharSequenceUtil.isNotBlank(comPort) && null != baudRate;
                break;
            default:
        }
        return success;
    }

    public String getHost() {
        return host;
    }

    public Integer getPort() {
        return port;
    }

    public String getComPort() {
        return comPort;
    }

    public Integer getBaudRate() {
        return baudRate;
    }

    @Override
    public String toString() {
        return "ConnectAddress{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", comPort='" + comPort + '\'' +
                ", baudrate=" + baudRate +
                '}';
    }
}
